package com.study;

import com.study.entity.User;
import org.springframework.beans.BeanUtils;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StreamTest02 {
    public static List<User> list = new ArrayList<>();
    public static Map<String, User> map = new HashMap<>();

    public static void main(String[] args) {
        //定义三个用户对象
        User user1 = new User();
        user1.setUsername("tom");
        user1.setPassword("1");
        User user2 = new User();
        user2.setUsername("jerry");
        user2.setPassword("06");
        User user3 = new User();
        user3.setUsername("mike");
        user3.setPassword("3");
        User user4 = new User();
        user4.setUsername("mike");
        user4.setPassword("4");

        //添加用户到集合中
        list.add(user1);
        list.add(user2);
        list.add(user3);
        list.add(user4);

        List<User> userList = list;

        //得出所有险别号
        List<String> usernames = userList.stream()
                .map(entry -> entry.getUsername())
                .collect(Collectors.toList());

        //得出重复的险别号(去重)
        List<String> repeatUserNames = usernames.stream()
                .collect(Collectors.groupingBy(c -> c, Collectors.counting())) //得出每个险别出现的次数
                .entrySet().stream() // Set<Entry>转换为Stream<Entry>
                .filter(entry -> entry.getValue() > 1) //过滤出元素出现次数大于 1 的 entry
                .map(entry -> entry.getKey()) //获得 entry 的键（重复元素）对应的 Stream
                .collect(Collectors.toList());//转化为List

        if (repeatUserNames.size() == 0){
            return;
        }

        repeatUserNames.stream().forEach(username ->{
            User user = userList.stream().filter(c -> c.getUsername().equals(username)).collect(Collectors.toList()).get(0);
            map.put(username, user);
        });

        //得出重复险别的险别号(不去重)
        List<String> usernameLIst = usernames.stream().filter(i -> {
            return repeatUserNames.contains(i);
        }).collect(Collectors.toList());

        //得出未重复险别的险别号
        List<String> noRepeats = usernames.stream().filter(i -> {
            return !repeatUserNames.contains(i);
        }).collect(Collectors.toList());

        //得出未重复的险别集合(不动)
        List<User> notRepeatList = noRepeats.stream().map(i -> {
            return userList.stream().filter(j -> {
                return j.getUsername().equals(i);
            }).collect(Collectors.toList()).get(0);
        }).collect(Collectors.toList());

        //遍历map,开始赋值,得出合并后的险别
        List<User> users = map.entrySet().stream().map(entry -> {
            //将map中的值清零
            User oldUser = (User) entry.getValue();
            User newUser = new User();
            try {
                BeanUtils.copyProperties(oldUser,newUser);
            }catch (Exception e){
                e.printStackTrace();
            }
            newUser.setPassword("789");
            //遍历原本cvrgList
            userList.stream().forEach(user -> {
                if (user.getUsername().equals(entry.getKey())) {
                    newUser.setPassword(newUser.getPassword() + "-" +user.getPassword());
                }
            });
            return newUser;
        }).collect(Collectors.toList());

        users.addAll(notRepeatList);
    }
}


